<template>
  <div class="directorys-add">
    <!-- 弹出框 -->
    <el-dialog :title="`${form.id?'修改':'新增'}目录`" :visible="showDialog" @close="btnCancel" @open='open'>
      <el-form ref="roleForm" :model="form" :rules="rules" label-width="100px">

        <el-form-item label="所属学科"  v-if="!$route.query.id" class="subjectName" prop="subjectName">
        <el-select v-model="form.subjectID" placeholder="请选择" style="width:100%">
        <el-option v-for="item in list" :key="item.id" :label="item.subjectName" :value="item.id"></el-option>
        <!-- <el-option label="区123" value="shanghai"></el-option>
        <el-option label="区域二" value="beijing"></el-option> -->
        </el-select>
      </el-form-item>
        <el-form-item label="目录名称" prop="directoryName">
        <el-input v-model="form.directoryName" placeholder="请输入目录名称" />
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button @click="btnCancel">取消</el-button>
        <el-button   type="primary" @click="headerCheckout">确认</el-button>
      </template>
    </el-dialog>

  </div>
</template>

<script>
import { update } from '../../api/hmmm/directorys'
export default {
  name: 'directorysAdd',
  props: ['list', 'row', 'aa'],
  data () {
    return {
      showDialog: false,
      form: { },

      rules: {
        subjectName: [// 校验新增
          { type: 'number', message: '请选择所属学科', tirgger: 'blur' }
        ],
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: ['change', 'blur'] }
        ]
      }
    }
  },
  created () {
    // console.log(this.row, '123')
  },
  methods: {
    open () {
      this.form = { ...this.row }
      // 清除校验效果
      this.$nextTick(() => {
        this.$refs.roleForm.clearValidate()
      })
    },
    // 添加显示和关闭的方法
    btnCancel () {
      this.showDialog = false
    },
    // 确认新增
    async headerCheckout () {
      // 修改
      if (this.form.id) {
        this.row.directoryName = this.form.directoryName
        this.row.subjectName = this.form.subjectName.trim()
        await update({
          id: this.form.id,
          subjectID: this.form.subjectID,
          directoryName: this.form.directoryName
        })
        this.$emit('subjectsList')
        this.form.directoryName = ''
      } else {
        // 新增
        if (!this.form.directoryName || !this.form.subjectID) {
          return this.$message('不能为空')
        }
        this.$emit('formClick', this.form)
        this.form = {
          id: null,
          subjectID: null,
          directoryName: ''
        }
      }
      this.$message.success('操作成功')
      this.showDialog = false
    }
  }
}
</script>

<style lang="less" scope>

</style>
